package com.mzp.XbShop.controller;

import com.mzp.XbShop.common.entity.Result;
import com.mzp.XbShop.common.entity.StatusCode;
import com.mzp.XbShop.pojo.UserCart;
import com.mzp.XbShop.service.UserCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author admin
 * @Description user_cart 控制器层
 * @date 2021-04-20 10:01:19
 */
@RestController
@CrossOrigin
@RequestMapping("/userCart")
public class UserCartController {

    @Autowired
    private UserCartService userCartService;

    /**
     * 查看该用户的所有购物
     * @param userid
     * @return
     */
    @GetMapping("/findByCartId/{userid}")
    public Result findByCartId(@PathVariable String userid){
        return new Result(true, StatusCode.OK,"查询成功",userCartService.findByCartId(userid));
    }


    /**
     * 查看该用户的所有等待支付购物
     * @param userid
     * @return
     */
    @GetMapping("/findByCartIdAndWaitPay/{userid}")
    public Result findByCartIdAndWaitPay(@PathVariable String userid){
        return new Result(true, StatusCode.OK,"查询成功",userCartService.findByCartIdAndWaitPay(userid));
    }

    /**
     * 添加收藏 addFavor
     */
    /**
     * 查看是否该用户id是否
     */
    @GetMapping("/exits/{goodsid}/{userid}")
    public Result exits(@PathVariable String goodsid,@PathVariable String userid){
        return new Result(true, StatusCode.OK,"查找成功",userCartService.exits(goodsid,userid));
    }

    /**
     * 添加购物车
     */
    @GetMapping("/addCart/{goodsid}/{userid}/{goodstotal}")
    public Result addCart(@PathVariable String goodsid,@PathVariable String userid,@PathVariable Integer goodstotal){
       Integer num= userCartService.addCart(goodsid,userid,goodstotal);
        return new Result(true, StatusCode.OK,"添加成功");
    }





    /**
     * 增加数量
     */
    @GetMapping("/updateCart/{goodsid}/{userid}/{goodstotal}")
    public Result updateCart(@PathVariable String goodsid,@PathVariable String userid,@PathVariable Integer goodstotal){
        Integer num= userCartService.updateCart(goodsid,userid,goodstotal);
        return new Result(true, StatusCode.OK,"添加成功");
    }


    /**
     * 删除该购物车
     */
    @GetMapping("/deleteCar/{goodsid}/{userid}")
    public Result deleteCar(@PathVariable String goodsid,@PathVariable String userid){
         userCartService.deleteCar(goodsid,userid);
        return new Result(true, StatusCode.OK,"删除成功");
    }

    /**
     * 删除待支付 购物车
     */
    @GetMapping("/deleteWaitCar/{id}/{userid}")
    public Result deleteWaitCar(@PathVariable String id,@PathVariable String userid){
        userCartService.deleteWaitCar(id,userid);
        return new Result(true, StatusCode.OK,"删除成功");
    }


    /**
     * 改变当前用户的 支付状态 从购物车 中  isin 字段改变 pay_status变为1
     * @param
     * @param
     * @return
     */
    @GetMapping("/subOrder/{goodsid}/{userid}/{goodstotal}")
    public Result subOrder(@PathVariable String goodsid,@PathVariable String userid,@PathVariable String goodstotal){
        System.out.println(goodsid+"--"+userid+"--"+goodstotal);
        userCartService.subOrder(goodsid,userid,goodstotal);
        return new Result(true, StatusCode.OK,"提交订单成功");
    }


}
